package cn.kp.learning.service.Impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.kp.learning.dao.GroupsDao;
import cn.kp.learning.dao.OrganizationDao;
import cn.kp.learning.model.Groups;
import cn.kp.learning.model.GroupsExample;
import cn.kp.learning.model.Organization;
import cn.kp.learning.model.OrganizationExample;
import cn.kp.learning.service.IdGeneratorService;
import cn.kp.learning.service.OrganizationService;

@Service("organizationService")
public class OrganizationServiceImpl implements OrganizationService {

	@Resource
	private OrganizationDao organizationDao;
	@Resource
	private GroupsDao groupsDao;
	@Resource
	private IdGeneratorService idGeneratorService;
	
	@Override
	public int insertGroup(Groups gp) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteGroup(String groupId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int modifyGroup(Groups gp) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int countByExample(GroupsExample example) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Groups> selectByExample(GroupsExample example) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int addOrganization(Organization org) {
		// TODO Auto-generated method stub
		HashMap<String,Object> hm = new HashMap<String, Object>();
		List<Groups> ltcompany = new ArrayList<Groups>();
		Groups gp = new Groups();
		int comflag = 0;
		int depflag = 0;
		int offflag = 0;
		int grpflag = 0;
		if(null==org.getOrgId()||org.getOrgId().equals("")){
			if(null!=org.getOrgCompany() && !org.getOrgCompany().equals("")){
				hm.put("groupsName", org.getOrgCompany());
				hm.put("groupsId", null);
				ltcompany =  groupsDao.selectByNamesAndId(hm);
				if(ltcompany.size()==1){
					org.setOrgCompanyId(ltcompany.get(0).getGroupsId());
					comflag=1;
				}else{
					int comresult = idGeneratorService.selectByCode("groupsComId");
					org.setOrgCompanyId(""+(1200000+comresult));
					gp.setGroupsName(org.getOrgCompany());
					gp.setGroupsId(org.getOrgCompanyId());
					comflag = groupsDao.insertSelective(gp);
				}
				if(null!=org.getOrgDepartment() && !org.getOrgDepartment().equals("")){
					hm.put("groupsName", org.getOrgDepartment());
					hm.put("groupsId", null);
					ltcompany =  groupsDao.selectByNamesAndId(hm);
					if(ltcompany.size()==1){
						org.setOrgDepartmentId(ltcompany.get(0).getGroupsId());
						depflag=1;
					}else{
						int depresult = idGeneratorService.selectByCode("groupsDepId");
						org.setOrgDepartmentId(""+(1300000+depresult));
						gp.setGroupsName(org.getOrgDepartment());
						gp.setGroupsId(org.getOrgDepartmentId());
						depflag = groupsDao.insertSelective(gp);
					}
					if(null!=org.getOrgOffice() && !org.getOrgOffice().equals("")){
						hm.put("groupsName", org.getOrgOffice());
						hm.put("groupsId", null);
						ltcompany =  groupsDao.selectByNamesAndId(hm);
						if(ltcompany.size()==1){
							org.setOrgOfficeId(ltcompany.get(0).getGroupsId());
							offflag=1;
						}else{
							int offresult = idGeneratorService.selectByCode("groupsOffId");
							org.setOrgOfficeId(""+(1400000+offresult));
							gp.setGroupsName(org.getOrgOffice());
							gp.setGroupsId(org.getOrgOfficeId());
							offflag = groupsDao.insertSelective(gp);
						}
						if(null!=org.getOrgGroup() && !org.getOrgGroup().equals("")){
							hm.put("groupsName", org.getOrgGroup());
							hm.put("groupsId", null);
							ltcompany =  groupsDao.selectByNamesAndId(hm);
							if(ltcompany.size()==1){
								org.setOrgGroupId(ltcompany.get(0).getGroupsId());
							}else{
								int grpresult = idGeneratorService.selectByCode("groupsGrpId");
								org.setOrgOfficeId(""+(1500000+grpresult));
								gp.setGroupsName(org.getOrgGroup());
								gp.setGroupsId(org.getOrgGroupId());
								grpflag = groupsDao.insertSelective(gp);
							}
						}else{
							//wrong input context. companyName cannot be null.
							System.out.println("group name cannot be null.[organizationServiceImpl-addOrganization]");
						}
					}else{
						//wrong input context. companyName cannot be null.
						System.out.println("office name cannot be null.[organizationServiceImpl-addOrganization]");
					}
				}else{
					//wrong input context. companyName cannot be null.
					System.out.println("department name cannot be null.[organizationServiceImpl-addOrganization]");
				}
			}else{
				//wrong input context. companyName cannot be null.
				System.out.println("company name cannot be null.[organizationServiceImpl-addOrganization]");
			}
			
		}else{
			
		}
		int orgaddresult = 0;
		if(comflag>0 && depflag>0 && offflag>0){
			int orgresult = idGeneratorService.selectByCode("groupsOrgId");
			org.setOrgId(""+(1100000+orgresult));
			orgaddresult = organizationDao.insertSelective(org);
		}else{
			
		}
		System.out.println("result [" + comflag + "]-[" + depflag + "]-[" + offflag + "]-[" + grpflag + "]" + " - " + orgaddresult);
		return orgaddresult;
	}

	@Override
	public int deleteOrganization(String orgId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int modifyOrganization(Organization org) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Organization getOrganization(Organization org) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int countByExample(OrganizationExample example) {
		// TODO Auto-generated method stub
		return organizationDao.countByExample(example);
	}

	@Override
	public List<Organization> selectByExample(OrganizationExample example) {
		// TODO Auto-generated method stub
		return organizationDao.selectByExample(example);
	}

	@Override
	public Organization selectByPrimaryKey(HashMap<String, Object> hm) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Organization> selectCompany(HashMap<String, Object> hm) {
		return organizationDao.selectCompany(hm);
	}

	@Override
	public List<Organization> selectDepartment(HashMap<String, Object> hm) {
		return organizationDao.selectDepartment(hm);
	}

	@Override
	public List<Organization> selectOffice(HashMap<String, Object> hm) {
		return organizationDao.selectOffice(hm);
	}

	@Override
	public List<Organization> selectGroup(HashMap<String, Object> hm) {
		return organizationDao.selectGroup(hm);
	}
	
	
}
